package com.qhit.jdbc;

import org.junit.jupiter.api.Test;

import java.sql.*;
import java.util.Scanner;

/**
 * TODO
 *
 * @author 村头老杨头
 * @version 1.0
 * @date 2021/9/26 9:18
 */
public class TestJDBC {
    private static String url = "jdbc:mysql://127.0.0.1:3306/edu?useSSL=false";
    private static String username = "root";
    private static String password = "123456";
//
//    public static void main(String[] args) throws ClassNotFoundException, SQLException {
//        //输入学生编号，查询学生信息，在控制台打印输出学生信息
//        //获取输入的学生编号
//        Scanner input = new Scanner(System.in);
//        System.out.println("请输入学生编号：");
//        String sno = input.nextLine();
//        //去数据库查询  select * from student where sno=?;
//        //1.加载驱动
//        Class.forName("com.mysql.cj.jdbc.Driver");
//        //2.创建连接
////        jdbc:mysql://127.0.0.1:3306/edu?useSSL=false   jdbc技术:连接的mysql数据库://IP地址:端口号/数据库名称?键=值&键=值
//        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/edu?useSSL=false", "root", "123456");
//        //3.创建SQL  SQL注入攻击
//        Statement st = conn.createStatement();
//        String sql = "select * from student where sno="+sno;
//        //4.发SQL返回结果
//        ResultSet rs = st.executeQuery(sql);
//        //5.处理结果
//        while (rs.next()){
//            //把查询的结果显示出来
//            System.out.println(rs.getString(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getDate(4) +"\t"+rs.getString(5));
//        }
//        //6.关闭连接
//        rs.close();
//        st.close();
//        conn.close();
//        //打印
//    }


//    @Test
//    public static void main(String[] args) throws ClassNotFoundException, SQLException {
//        Scanner input = new Scanner(System.in);
//        System.out.println("请输入用户名");
//        String user = input.nextLine();
//        System.out.println("请输入密码");
//        String pwd = input.nextLine();
//
//        Class.forName("com.mysql.cj.jdbc.Driver");
//        Connection conn = DriverManager.getConnection(url, username, password);
//        Statement st = conn.createStatement();
//        String sql = "select * from student where sname='"+user+"' and sno="+pwd;
//        System.out.println(sql);
//        ResultSet rs = st.executeQuery(sql);
//
//        if(rs.next()){
//            System.out.println("登录成功");
//        }else{
//            System.out.println("登录失败");
//        }
//        rs.close();
//        st.close();
//        conn.close();
//
//    }

    @Test
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入用户名");
        String user = input.nextLine();
        System.out.println("请输入密码");
        String pwd = input.nextLine();

        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection(url, username, password);
        String sql = "select * from student where sname=? and sno=?";
        //预编译SQL
        PreparedStatement ps = conn.prepareStatement(sql);
        //填充参数
        ps.setString(1,user);
        ps.setString(2,pwd);
        //发送SQL语句
        ResultSet rs = ps.executeQuery();
        if(rs.next()){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }
        rs.close();
        ps.close();
        conn.close();
    }
}
